package com.juttec.freshbird.pet.base.baseimpl;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;

import com.juttec.freshbird.pet.MainActivity;
import com.juttec.freshbird.pet.R;
import com.juttec.freshbird.pet.SearchActivity;
import com.juttec.freshbird.pet.base.BasePager;
import com.juttec.freshbird.pet.base.BaseTab;
import com.juttec.freshbird.pet.base.zixunbasetab.ZixunLatedTab;
import com.juttec.freshbird.pet.base.zixunbasetab.ZixunRestTab;
import com.juttec.freshbird.pet.fragment.LeftFragment;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.viewpagerindicator.TabPageIndicator;

import java.util.ArrayList;

/**
 * Created by Administrator on 2015/10/21.
 */
public class ZixunPager extends BasePager {

    //测试用资讯论坛indicator数据
    public static final String[] test = new String[]{"最新", "快报", "视频", "新闻", "文化", "说客",
            "导购", "原创视频", "比赛", "竞技", "科技", "养宠心德", "幽默搞笑", "海外资讯"};
    public ZixunLatedTab mZixunLatedTab;
    public ArrayList<ZixunRestTab> mZixunRestPagerList;
    private ViewPager mViewPager;
    private TabPageIndicator mIndicator;
    private BaseTab mBaseTab;

    public ZixunPager(Activity activity) {
        super(activity);
    }

    @Override
    public View initViews() {
        View view = View.inflate(mActivity, R.layout.page_zixun, null);
        mViewPager = (ViewPager) view.findViewById(R.id.vp_zixun);
//        初始化TabPageIndicator
        mIndicator = (TabPageIndicator) view.findViewById(R.id.zixun_indicator);
        ViewUtils.inject(this, view);
        mViewPager.setAdapter(new ZixunTabMenuAdapter());
        return view;
    }

    @Override
    public void initData() {

        mZixunLatedTab = new ZixunLatedTab(mActivity);
        mZixunRestPagerList = new ArrayList<ZixunRestTab>();
        for (int i = 0; i < 13; i++) {
            ZixunRestTab zixunRestTab = new ZixunRestTab(mActivity);
            mZixunRestPagerList.add(zixunRestTab);
        }
        //将viewpager和indicator关联起来，必须在viewpager设置adapter之后关联
        //否则，当viewpager没有设置adapter就关联，会报出空指针异常
        mIndicator.setViewPager(mViewPager);
        mIndicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mBaseTab.initData();
                if (position == 0) {
                    setSlidingMenuLeftEnable();
                } else {
                    setSlidingMenuNone();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        setSlidingMenuLeftEnable();
//        mZixunLatedTab.initData();
    }

    @OnClick(R.id.btn_right)
    public void nextPage(View view) {
        int currentItem = mViewPager.getCurrentItem();
        mViewPager.setCurrentItem(++currentItem);
    }

    @OnClick(R.id.btn_search)
    public void setBtnSearch(View view){
        Intent intent = new Intent(mActivity, SearchActivity.class);
        mActivity.startActivity(intent);
    }

    class ZixunTabMenuAdapter extends PagerAdapter {

        /**
         * 重写此方法，返回页面主题，用于viewpagerindicator的页签显示
         *
         * @param position
         * @return
         */
        @Override
        public CharSequence getPageTitle(int position) {
            return test[position];
        }

        @Override
        public int getCount() {
            //返回indicator对象数量
            return test.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            if (position == 0) {
                mBaseTab = mZixunLatedTab;
            } else {
                mBaseTab = mZixunRestPagerList.get(position-1);
            }
            container.addView(mBaseTab.mRootView);
            mBaseTab.initData();
            return mBaseTab.mRootView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }

}
